<template>
    <h3>修改借款信息</h3>
    <table>
        <tr>
            <td>借款款项名称</td>
            <td><input type="text" v-model="JName"></td>
        </tr>
        <tr>
            <td>借款最高金额</td>
            <td><input type="text" v-model="MaxMoney"></td>
        </tr>
        <tr>
            <td>借款最高年限</td>
            <td><input type="text" v-model="MaxYear"></td>
        </tr>
        <tr>
            <td>利息</td>
            <td>
                <select v-model="IId">
                    <option value="0">请选择</option>
                    <option v-for="item in str" :key="item.IId" :value="item.IId">
                        {{ item.IInverest }}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>借款款项简介</td>
            <td><input type="TextArea" v-model="Info"></td>
        </tr>
        <tr>
            <td colspan="2"><button @click="UptJieKuan">修改借款信息</button></td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import axios from 'axios';
import { useRoute } from "vue-router";
const route = useRoute();
const JId = ref(route.params.JId);
const JName = ref("");
const MaxMoney = ref();
const MaxYear = ref("");
const Info = ref("");
const IId = ref('0');
//绑定下拉
const str = ref([]);
onMounted(() => {
    axios.get('http://localhost:62489/api/Jie/BindInverest', {
        params: {
            IId: IId
        }
    })
        .then(res => {
            str.value = res.data;
            console.log(res.data);
        })
        .catch(error => {
            console.log(error);
        })
})
//反填
onMounted(() => {
    axios.get('http://localhost:62489/api/Jie/Find', {
        params: {
            jid: JId.value
        }
    })
        .then(res => {
            JName.value = res.data.JName;
            MaxMoney.value = res.data.MaxMoney;
            MaxYear.value = res.data.MaxYear;
            Info.value = res.data.Info;
            IId.value = res.data.IId;
        })
        .catch(error => {
            console.log(error);
        })
})
//修改借款信息
const UptJieKuan = () => {
    if (MaxMoney.value <= 0) {
        alert("最高借款金额为数字且必须大于0")
        return;
    }
    if (!JName.value) {
        alert("名称不能为空")
        return;
    }
    if (!MaxMoney.value) {
        alert("最高借款金额不能为空")
        return;
    }
    if (!MaxYear.value) {
        alert("借款最高年限不能为空")
        return;
    }
    if (!Info.value) {
        alert("借款款项简介不能为空")
        return;
    }
    axios.post('http://localhost:62489/api/Jie/Update', {
        JName: JName.value,
        MaxMoney: MaxMoney.value,
        MaxYear: MaxYear.value,
        Info: Info.value,
        IId: IId.value
    })
        .then(res => {
            if (res.data > 0) {
                alert("修改成功")
                location.href = "/Show";
            }
            else {
                alert("修改失败")
                return;
            }
        })
        .catch(error => {
            console.log(error);
        })
}

</script>